home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 3 / Gold Medal Software - Volume 3 (Gold Medal) (1994).iso / comms / rzsz0306.arj / SZ.C < prev    next >
C/C++ Source or Header  |  1994-02-24  |  34KB  |  1,559 lines

  1. #define VERSION "3.34 02-24-94"
  2. #define PUBDIR "/usr/spool/uucppublic"
  3.  
  4. /*
  5.  **************************************************************************
  6.  *
  7.  * sz.c By Chuck Forsberg,  Omen Technology INC
  8.  *    Copyright 1994 Omen Technology Inc All Rights Reserved
  9.  *
  10.  * 
  11.  *    This version implements numerous enhancements including ZMODEM
  12.  *    Run Length Encoding and variable length headers.  These
  13.  *    features were not funded by the original Telenet development
  14.  *    contract.
  15.  * 
  16.  *  This software may be freely used for educational (didactic
  17.  *  only) purposes.  This software may also be freely used to
  18.  *  support file transfer operations to or from licensed Omen
  19.  *  Technology products.  Use with other commercial or shareware
  20.  *  programs (Crosstalk, Procomm, etc.) REQUIRES REGISTRATION.
  21.  *
  22.  *  Any programs which incorporate part or all of this code must be
  23.  *  provided in source form with this notice intact except by
  24.  *  prior written permission from Omen Technology Incorporated.
  25.  *  This includes compiled executables of this program.
  26.  *
  27.  *   The .doc files and the file "mailer.rz" must also be included.
  28.  * 
  29.  * Use of this software for commercial or administrative purposes
  30.  * except when exclusively limited to interfacing Omen Technology
  31.  * products requires a per port license payment of $20.00 US per
  32.  * port (less in quantity, see mailer.rz).  Use of this code by
  33.  * inclusion, decompilation, reverse engineering or any other means
  34.  * constitutes agreement to these conditions and acceptance of
  35.  * liability to license the materials and payment of reasonable
  36.  * legal costs necessary to enforce this license agreement.
  37.  *
  38.  *
  39.  *        Omen Technology Inc
  40.  *        Post Office Box 4681
  41.  *        Portland OR 97208
  42.  *
  43.  *    This code is made available in the hope it will be useful,
  44.  *    BUT WITHOUT ANY WARRANTY OF ANY KIND OR LIABILITY FOR ANY
  45.  *    DAMAGES OF ANY KIND.
  46.  *
  47.  *  USG UNIX (3.0) ioctl conventions courtesy Jeff Martin
  48.  */
  49.  
  50. char *Copyrsz = "Copyright 1994 Omen Technology Inc All Rights Reserved";
  51.  
  52. char *substr();
  53.  
  54. #define LOGFILE "/tmp/szlog"
  55. #define LOGFILE2 "szlog"
  56. #include <stdio.h>
  57. #include <signal.h>
  58. #include <ctype.h>
  59. #include <errno.h>
  60. extern int errno;
  61. #define STATIC
  62.  
  63. #define PATHLEN 256
  64. #define OK 0
  65. #define FALSE 0
  66. #ifdef TRUE
  67. #undef TRUE
  68. #endif
  69. #define TRUE 1
  70. #define ERROR (-1)
  71. /* Ward Christensen / CP/M parameters - Don't change these! */
  72. #define ENQ 005
  73. #define CAN ('X'&037)
  74. #define XOFF ('s'&037)
  75. #define XON ('q'&037)
  76. #define SOH 1
  77. #define STX 2
  78. #define EOT 4
  79. #define ACK 6
  80. #define NAK 025
  81. #define CPMEOF 032
  82. #define WANTCRC 0103    /* send C not NAK to get crc not checksum */
  83. #define WANTG 0107    /* Send G not NAK to get nonstop batch xmsn */
  84. #define TIMEOUT (-2)
  85. #define RCDO (-3)
  86. #define GCOUNT (-4)
  87. #define RETRYMAX 10
  88.  
  89.  
  90. #define HOWMANY 2
  91. STATIC int Zmodem=0;        /* ZMODEM protocol requested by receiver */
  92. unsigned Baudrate = 9600;        /* Default, set by first mode() call */
  93. STATIC unsigned Effbaud = 9600;
  94. STATIC unsigned Txwindow;    /* Control the size of the transmitted window */
  95. STATIC unsigned Txwspac;    /* Spacing between zcrcq requests */
  96. STATIC unsigned Txwcnt;    /* Counter used to space ack requests */
  97. STATIC long Lrxpos;    /* Receiver's last reported offset */
  98. STATIC int errors;
  99. char endmsg[80] = {0};    /* Possible message to display on exit */
  100.  
  101. #include "rbsb.c"    /* most of the system dependent stuff here */
  102.  
  103. #include "crctab.c"
  104.  
  105. STATIC int Filesleft;
  106. STATIC long Totalleft;
  107.  
  108. /*
  109.  * Attention string to be executed by receiver to interrupt streaming data
  110.  *  when an error is detected.  A pause (0336) may be needed before the
  111.  *  ^C (03) or after it.
  112.  */
  113. #ifdef READCHECK
  114. STATIC char Myattn[] = { 0 };
  115. #else
  116. #ifdef USG
  117. STATIC char Myattn[] = { 03, 0336, 0 };
  118. #endif
  119. #endif
  120.  
  121. FILE *in;
  122.  
  123. STATIC int Canseek = 1;    /* 1: Can seek 0: only rewind -1: neither (pipe) */
  124.  
  125. #ifdef TXBSIZE
  126. #define TXBMASK (TXBSIZE-1)
  127. STATIC char Txb[TXBSIZE];        /* Circular buffer for file reads */
  128. STATIC char *txbuf = Txb;        /* Pointer to current file segment */
  129. #else
  130. STATIC char txbuf[1024];
  131. #endif
  132. STATIC long vpos = 0;        /* Number of bytes read from file */
  133.  
  134. STATIC char Lastrx;
  135. STATIC char Crcflg;
  136. STATIC int Modem2=0;        /* XMODEM Protocol - don't send pathnames */
  137. STATIC int Restricted=0;    /* restricted; no /.. or ../ in filenames */
  138. STATIC int Fullname=0;        /* transmit full pathname */
  139. STATIC int Unlinkafter=0;    /* Unlink file after it is sent */
  140. STATIC int Dottoslash=0;    /* Change foo.bar.baz to foo/bar/baz */
  141. STATIC int firstsec;
  142. STATIC int errcnt=0;        /* number of files unreadable */
  143. STATIC int blklen=128;        /* length of transmitted records */
  144. STATIC int Optiong;        /* Let it rip no wait for sector ACK's */
  145. STATIC int Eofseen;        /* EOF seen on input set by zfilbuf */
  146. STATIC int BEofseen;        /* EOF seen on input set by fooseek */
  147. STATIC int Totsecs;        /* total number of sectors this file */
  148. STATIC int Filcnt=0;        /* count of number of files opened */
  149. STATIC unsigned Rxbuflen=16384;    /* Receiver's max buffer length */
  150. STATIC long Tframlen = 0;    /* Override for tx frame length */
  151. STATIC int blkopt=0;        /* Override value for zmodem blklen */
  152. STATIC int Rxflags = 0;
  153. STATIC long bytcnt;
  154. STATIC int Wantfcs32 = TRUE;    /* want to send 32 bit FCS */
  155. STATIC char Lzconv;    /* Local ZMODEM file conversion request */
  156. STATIC char Lzmanag;    /* Local ZMODEM file management request */
  157. STATIC int Lskipnocor;
  158. STATIC char Lztrans;
  159. STATIC int Command;        /* Send a command, then exit. */
  160. STATIC char *Cmdstr;        /* Pointer to the command string */
  161. STATIC int Cmdack1;        /* Rx ACKs command, then do it */
  162. STATIC int Exitcode;
  163. STATIC int Test;        /* 1= Force receiver to send Attn, etc with qbf. */
  164.             /* 2= Character transparency test */
  165. STATIC char *qbf=
  166.  "The quick brown fox jumped over the lazy dog's back 1234567890\r\n";
  167. STATIC long Lastsync;    /* Last offset to which we got a ZRPOS */
  168. STATIC int Beenhereb4;        /* How many times we've been ZRPOS'd here */
  169. STATIC int Ksendstr;        /* 1= Send esc-?-3-4-l to remote kermit */
  170. STATIC char *ksendbuf = "\033[?34l";
  171.  
  172. STATIC jmp_buf tohere;        /* For the interrupt on RX timeout */
  173. STATIC jmp_buf intrjmp;    /* For the interrupt on RX CAN */
  174.  
  175.  
  176. /* called by signal interrupt or terminate to clean things up */
  177. void
  178. bibi(n)
  179. {
  180.     canit(); fflush(stdout); mode(0);
  181.     fprintf(stderr, "sz: caught signal %d; exiting\n", n);
  182.     if (n == SIGQUIT)
  183.         abort();
  184.     if (n == 99)
  185.         fprintf(stderr, "mode(2) in rbsb.c not implemented!!\n");
  186.     exit(3);
  187. }
  188.  
  189. /* Called when ZMODEM gets an interrupt (^X) */
  190. void
  191. onintr(c)
  192. {
  193.     signal(SIGINT, SIG_IGN);
  194.     longjmp(intrjmp, -1);
  195. }
  196.  
  197. STATIC int Zctlesc;    /* Encode control characters */
  198. STATIC int Nozmodem = 0;    /* If invoked as "sb" */
  199. STATIC char *Progname = "sz";
  200. STATIC int Zrwindow = 1400;    /* RX window size (controls garbage count) */
  201.  
  202. /*
  203.  * Log an error
  204.  */
  205. /*VARARGS1*/
  206. void
  207. zperr(s,p,u)
  208. char *s, *p, *u;
  209. {
  210.     if (Verbose <= 0)
  211.         return;
  212.     fprintf(stderr, "Retry %d: ", errors);
  213.     fprintf(stderr, s, p, u);
  214.     fprintf(stderr, "\n");
  215. }
  216.  
  217. #include "zm.c"
  218. #include "zmr.c"
  219.  
  220. main(argc, argv)
  221. char *argv[];
  222. {
  223.     register char *cp;
  224.     register npats;
  225.     char **patts;
  226.  
  227.     if ((cp = getenv("ZNULLS")) && *cp)
  228.         Znulls = atoi(cp);
  229.     if ((cp=getenv("SHELL")) && (substr(cp, "rsh") || substr(cp, "rksh")))
  230.         Restricted=TRUE;
  231.     inittty();
  232.     chkinvok(argv[0]);
  233.  
  234.     Rxtimeout = 600;
  235.     npats=0;
  236.     if (argc<2)
  237.         usage();
  238.     while (--argc) {
  239.         cp = *++argv;
  240.         if (*cp++ == '-' && *cp) {
  241.             while ( *cp) {
  242.                 if (isdigit(*cp)) {
  243.                     ++cp;  continue;
  244.                 }
  245.                 switch(*cp++) {
  246.                 case '\\':
  247.                      *cp = toupper(*cp);  continue;
  248.                 case '+':
  249.                     Lzmanag = ZMAPND; break;
  250.                 case 'a':
  251.                     if (Nozmodem || Modem2)
  252.                         usage();
  253.                     Lzconv = ZCNL;  break;
  254.                 case 'b':
  255.                     Lzconv = ZCBIN; break;
  256.                 case 'c':
  257.                     Lzmanag = ZMCHNG;  break;
  258.                 case 'd':
  259.                     ++Dottoslash;
  260.                     /* **** FALL THROUGH TO **** */
  261.                 case 'f':
  262.                     Fullname=TRUE; break;
  263.                         case 'g' :
  264.                     Ksendstr = TRUE; break;
  265.                 case 'e':
  266.                     Zctlesc = 1; break;
  267.                 case 'k':
  268.                     blklen=1024; break;
  269.                 case 'L':
  270.                     if (isdigit(*cp))
  271.                         blkopt = atoi(cp);
  272.                     else {
  273.                         if (--argc < 1)
  274.                             usage();
  275.                         blkopt = atoi(*++argv);
  276.                     }
  277.                     if (blkopt<24 || blkopt>1024)
  278.                         usage();
  279.                     break;
  280.                 case 'l':
  281.                     if (isdigit(*cp))
  282.                         Tframlen = atol(cp);
  283.                     else {
  284.                         if (--argc < 1)
  285.                             usage();
  286.                         Tframlen = atol(*++argv);
  287.                     }
  288.                     if (Tframlen<32 || Tframlen>65535L)
  289.                         usage();
  290.                     break;
  291.                 case 'N':
  292.                     Lzmanag = ZMNEWL;  break;
  293.                 case 'n':
  294.                     Lzmanag = ZMNEW;  break;
  295.                 case 'o':
  296.                     Wantfcs32 = FALSE; break;
  297.                 case 'p':
  298.                     Lzmanag = ZMPROT;  break;
  299.                 case 'r':
  300.                     if (Lzconv == ZCRESUM)
  301.                         Lzmanag = (Lzmanag & ZMMASK) | ZMCRC;
  302.                     Lzconv = ZCRESUM; break;
  303.                 case 'T':
  304.                     chartest(1); chartest(2);
  305.                     mode(0);  exit(0);
  306.                 case 'u':
  307.                     ++Unlinkafter; break;
  308.                 case 'v':
  309.                     ++Verbose; break;
  310.                 case 'w':
  311.                     if (isdigit(*cp))
  312.                         Txwindow = atoi(cp);
  313.                     else {
  314.                         if (--argc < 1)
  315.                             usage();
  316.                         Txwindow = atoi(*++argv);
  317.                     }
  318.                     if (Txwindow < 256)
  319.                         Txwindow = 256;
  320.                     Txwindow = (Txwindow/64) * 64;
  321.                     Txwspac = Txwindow/4;
  322.                     if (blkopt > Txwspac
  323.                      || (!blkopt && Txwspac < 1024))
  324.                         blkopt = Txwspac;
  325.                     break;
  326.                 case 'Y':
  327.                     Lskipnocor = TRUE;
  328.                     /* **** FALLL THROUGH TO **** */
  329.                 case 'y':
  330.                     Lzmanag = ZMCLOB; break;
  331.                 case 'Z':
  332.                 case 'z':
  333.                     Lztrans = ZTRLE;  break;
  334.                 default:
  335.                     usage();
  336.                 }
  337.             }
  338.         }
  339.         else if (Command) {
  340.             if (argc != 1) {
  341.                 usage();
  342.             }
  343.             Cmdstr = *argv;
  344.         }
  345.         else if ( !npats && argc>0) {
  346.             if (argv[0][0]) {
  347.                 npats=argc;
  348.                 patts=argv;
  349.             }
  350.         }
  351.     }
  352.     if (npats < 1 && !Command && !Test) 
  353.         usage();
  354.     if (Verbose) {
  355.         if (freopen(LOGFILE, "a", stderr)==NULL)
  356.             if (freopen(LOGFILE2, "a", stderr)==NULL) {
  357.                 printf("Can't open log file!");
  358.                 exit(2);
  359.             }
  360.         setbuf(stderr, NULL);
  361.     }
  362.     vfile("%s %s for %s tty=%s\n", Progname, VERSION, OS, Nametty);
  363.  
  364.     mode(3);
  365.  
  366.     if (signal(SIGINT, bibi) == SIG_IGN) {
  367.         signal(SIGINT, SIG_IGN); signal(SIGKILL, SIG_IGN);
  368.     } else {
  369.         signal(SIGINT, bibi); signal(SIGKILL, bibi);
  370.     }
  371. #ifdef SIGQUIT
  372.     signal(SIGQUIT, SIG_IGN);
  373. #endif
  374. #ifdef SIGTERM
  375.     signal(SIGTERM, bibi);
  376. #endif
  377.  
  378.     countem(npats, patts);
  379.  
  380.     if (!Modem2 && !Nozmodem) {
  381.         if (Ksendstr)
  382.             printf(ksendbuf);
  383.         printf("rz\r");  fflush(stdout);
  384.         stohdr(0L);
  385.         if (Command)
  386.             Txhdr[ZF0] = ZCOMMAND;
  387.         zshhdr(4, ZRQINIT, Txhdr);
  388.     }
  389.     fflush(stdout);
  390.  
  391.  
  392.     if (Command) {
  393.         if (getzrxinit()) {
  394.             Exitcode=1; canit();
  395.         }
  396.         else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  397.             Exitcode=1; canit();
  398.         }
  399.     } else if (wcsend(npats, patts)==ERROR) {
  400.         Exitcode=1;
  401.         canit();
  402.     }
  403.     if (endmsg[0]) {
  404.         printf("  %s: %s\r\n", Progname, endmsg);
  405.         if (Verbose)
  406.             fprintf(stderr, "%s\r\n", endmsg);
  407.     }
  408.     printf("%s %s finished.\r\n", Progname, VERSION);
  409.     fflush(stdout);
  410.     mode(0);
  411.     if(errcnt || Exitcode)
  412.         exit(1);
  413.  
  414. #ifndef REGISTERED
  415.     /* Removing or disabling this code without registering is theft */
  416.     if (!Usevhdrs)  {
  417.         printf("\n\n\nPlease read the License Agreement in sz.doc\n");
  418.         fflush(stdout);
  419.         sleep(10);
  420.     }
  421. #endif
  422.     exit(0);
  423.     /*NOTREACHED*/
  424. }
  425.  
  426. /* Say "bibi" to the receiver, try to do it cleanly */
  427. void
  428. saybibi()
  429. {
  430.     for (;;) {
  431.         stohdr(0L);        /* CAF Was zsbhdr - minor change */
  432.         zshhdr(4, ZFIN, Txhdr);    /*  to make debugging easier */
  433.         switch (zgethdr(Rxhdr)) {
  434.         case ZFIN:
  435.             sendline('O'); sendline('O'); flushmo();
  436.         case ZCAN:
  437.         case TIMEOUT:
  438.             return;
  439.         }
  440.     }
  441. }
  442.  
  443. wcsend(argc, argp)
  444. char *argp[];
  445. {
  446.     register n;
  447.  
  448.     Crcflg=FALSE;
  449.     firstsec=TRUE;
  450.     bytcnt = -1;
  451.     if (Nozmodem) {
  452.         printf("Start your local YMODEM receive.     ");
  453.         fflush(stdout);
  454.     }
  455.     for (n=0; n<argc; ++n) {
  456.         Totsecs = 0;
  457.         if (wcs(argp[n])==ERROR)
  458.             return ERROR;
  459.     }
  460.     Totsecs = 0;
  461.     if (Filcnt==0) {    /* bitch if we couldn't open ANY files */
  462.         if (!Nozmodem && !Modem2) {
  463.             Command = TRUE;
  464.             Cmdstr = "echo \"sz: Can't open any requested files\"";
  465.             if (getnak()) {
  466.                 Exitcode=1; canit();
  467.             }
  468.             if (!Zmodem)
  469.                 canit();
  470.             else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  471.                 Exitcode=1; canit();
  472.             }
  473.             Exitcode = 1; return OK;
  474.         }
  475.         canit();
  476.         sprintf(endmsg, "Can't open any requested files");
  477.         return ERROR;
  478.     }
  479.     if (Zmodem)
  480.         saybibi();
  481.     else if ( !Modem2)
  482.         wctxpn("");
  483.     return OK;
  484. }
  485.  
  486. wcs(oname)
  487. char *oname;
  488. {
  489.     register c;
  490.     struct stat f;
  491.     char name[PATHLEN];
  492.  
  493.     strcpy(name, oname);
  494.  
  495.     if (Restricted) {
  496.         /* restrict pathnames to current tree or uucppublic */
  497.         if ( substr(name, "../")
  498.          || (name[0]== '/' && strncmp(name, PUBDIR, strlen(PUBDIR))) ) {
  499.             canit();  sprintf(endmsg,"Security Violation");
  500.             return ERROR;
  501.         }
  502.     }
  503.  
  504.     in=fopen(oname, "r");
  505.  
  506.     if (in==NULL) {
  507.         ++errcnt;
  508.         return OK;    /* pass over it, there may be others */
  509.     }
  510.     BEofseen = Eofseen = 0;  vpos = 0;
  511.  
  512.     /* Check for directory */
  513.     fstat(fileno(in), &f);
  514. #ifdef POSIX
  515.     if (S_ISDIR(f.st_mode))
  516. #else
  517.     c = f.st_mode & S_IFMT;
  518.     if (c == S_IFDIR || c == S_IFBLK)
  519. #endif
  520.     {
  521.         fclose(in);
  522.         return OK;
  523.     }
  524.  
  525.     ++Filcnt;
  526.     switch (wctxpn(name)) {
  527.     case ZSKIP:
  528.     case ZFERR:
  529.         return OK;
  530.     case OK:
  531.         break;
  532.     default:
  533.         return ERROR;
  534.     }
  535.     if (!Zmodem && wctx(f.st_size))
  536.         return ERROR;
  537.  
  538.     if (Unlinkafter)
  539.         unlink(oname);
  540.  
  541.     return 0;
  542. }
  543.  
  544. /*
  545.  * generate and transmit pathname block consisting of
  546.  *  pathname (null terminated),
  547.  *  file length, mode time and file mode in octal
  548.  *  as provided by the Unix fstat call.
  549.  *  N.B.: modifies the passed name, may extend it!
  550.  */
  551. wctxpn(name)
  552. char *name;
  553. {
  554.     register char *p, *q;
  555.     char name2[PATHLEN];
  556.     struct stat f;
  557.  
  558.     if (Modem2) {
  559.         if (*name && fstat(fileno(in), &f)!= -1) {
  560.             fprintf(stderr, "Sending %s, %ld XMODEM blocks. ",
  561.               name, (127+f.st_size)>>7);
  562.         }
  563.         printf("Start your local XMODEM receive.     ");
  564.         fflush(stdout);
  565.         return OK;
  566.     }
  567.     zperr("Awaiting pathname nak for %s", *name?name:"<END>");
  568.     if ( !Zmodem)
  569.         if (getnak())
  570.             return ERROR;
  571.  
  572.     q = (char *) 0;
  573.     if (Dottoslash) {        /* change . to . */
  574.         for (p=name; *p; ++p) {
  575.             if (*p == '/')
  576.                 q = p;
  577.             else if (*p == '.')
  578.                 *(q=p) = '/';
  579.         }
  580.         if (q && strlen(++q) > 8) {    /* If name>8 chars */
  581.             q += 8;            /*   make it .ext */
  582.             strcpy(name2, q);    /* save excess of name */
  583.             *q = '.';
  584.             strcpy(++q, name2);    /* add it back */
  585.         }
  586.     }
  587.  
  588.     for (p=name, q=txbuf ; *p; )
  589.         if ((*q++ = *p++) == '/' && !Fullname)
  590.             q = txbuf;
  591.     *q++ = 0;
  592.     p=q;
  593.     while (q < (txbuf + 1024))
  594.         *q++ = 0;
  595.     if (*name) {
  596.         if (fstat(fileno(in), &f)!= -1)
  597.             sprintf(p, "%lu %lo %o 0 %d %ld", f.st_size, f.st_mtime,
  598.               f.st_mode, Filesleft, Totalleft);
  599.         Totalleft -= f.st_size;
  600.     }
  601.     if (--Filesleft <= 0)
  602.         Filesleft = Totalleft = 0;
  603.     if (Totalleft < 0)
  604.         Totalleft = 0;
  605.  
  606.     /* force 1k blocks if name won't fit in 128 byte block */
  607.     if (txbuf[125])
  608.         blklen=1024;
  609.     else {        /* A little goodie for IMP/KMD */
  610.         txbuf[127] = (f.st_size + 127) >>7;
  611.         txbuf[126] = (f.st_size + 127) >>15;
  612.     }
  613.     if (Zmodem)
  614.         return zsendfile(txbuf, 1+strlen(p)+(p-txbuf));
  615.     if (wcputsec(txbuf, 0, 128)==ERROR)
  616.         return ERROR;
  617.     return OK;
  618. }
  619.  
  620. getnak()
  621. {
  622.     register firstch;
  623.  
  624.     Lastrx = 0;
  625.     for (;;) {
  626.         switch (firstch = readline(800)) {
  627.         case ZPAD:
  628.             if (getzrxinit())
  629.                 return ERROR;
  630.             return FALSE;
  631.         case TIMEOUT:
  632.             sprintf(endmsg, "Timeout waiting for ZRINIT");
  633.             return TRUE;
  634.         case WANTG:
  635. #ifdef MODE2OK
  636.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  637. #endif
  638.             Optiong = TRUE;
  639.             blklen=1024;
  640.         case WANTCRC:
  641.             Crcflg = TRUE;
  642.         case NAK:
  643.             return FALSE;
  644.         case CAN:
  645.             if ((firstch = readline(20)) == CAN && Lastrx == CAN) {
  646.                 sprintf(endmsg, "Got CAN waiting to send file");
  647.                 return TRUE;
  648.             }
  649.         default:
  650.             break;
  651.         }
  652.         Lastrx = firstch;
  653.     }
  654. }
  655.  
  656.  
  657. wctx(flen)
  658. long flen;
  659. {
  660.     register int thisblklen;
  661.     register int sectnum, attempts, firstch;
  662.     long charssent;
  663.  
  664.     charssent = 0;  firstsec=TRUE;  thisblklen = blklen;
  665.     vfile("wctx:file length=%ld", flen);
  666.  
  667.     while ((firstch=readline(Rxtimeout))!=NAK && firstch != WANTCRC
  668.       && firstch != WANTG && firstch!=TIMEOUT && firstch!=CAN)
  669.         ;
  670.     if (firstch==CAN) {
  671.         zperr("Receiver CANcelled");
  672.         return ERROR;
  673.     }
  674.     if (firstch==WANTCRC)
  675.         Crcflg=TRUE;
  676.     if (firstch==WANTG)
  677.         Crcflg=TRUE;
  678.     sectnum=0;
  679.     for (;;) {
  680.         if (flen <= (charssent + 896L))
  681.             thisblklen = 128;
  682.         if ( !filbuf(txbuf, thisblklen))
  683.             break;
  684.         if (wcputsec(txbuf, ++sectnum, thisblklen)==ERROR)
  685.             return ERROR;
  686.         charssent += thisblklen;
  687.     }
  688.     fclose(in);
  689.     attempts=0;
  690.     do {
  691.         purgeline();
  692.         sendline(EOT);
  693.         flushmo();
  694.         ++attempts;
  695.     }
  696.         while ((firstch=(readline(Rxtimeout)) != ACK) && attempts < RETRYMAX);
  697.     if (attempts == RETRYMAX) {
  698.         zperr("No ACK on EOT");
  699.         return ERROR;
  700.     }
  701.     else
  702.         return OK;
  703. }
  704.  
  705. wcputsec(buf, sectnum, cseclen)
  706. char *buf;
  707. int sectnum;
  708. int cseclen;    /* data length of this sector to send */
  709. {
  710.     register checksum, wcj;
  711.     register char *cp;
  712.     unsigned oldcrc;
  713.     int firstch;
  714.     int attempts;
  715.  
  716.     firstch=0;    /* part of logic to detect CAN CAN */
  717.  
  718.     if (Verbose>2)
  719.         fprintf(stderr, "Sector %3d %2dk\n", Totsecs, Totsecs/8 );
  720.     else if (Verbose>1)
  721.         fprintf(stderr, "\rSector %3d %2dk ", Totsecs, Totsecs/8 );
  722.     for (attempts=0; attempts <= RETRYMAX; attempts++) {
  723.         Lastrx= firstch;
  724.         sendline(cseclen==1024?STX:SOH);
  725.         sendline(sectnum);
  726.         sendline(-sectnum -1);
  727.         oldcrc=checksum=0;
  728.         for (wcj=cseclen,cp=buf; --wcj>=0; ) {
  729.             sendline(*cp);
  730.             oldcrc=updcrc((0377& *cp), oldcrc);
  731.             checksum += *cp++;
  732.         }
  733.         if (Crcflg) {
  734.             oldcrc=updcrc(0,updcrc(0,oldcrc));
  735.             sendline((int)oldcrc>>8);
  736.             sendline((int)oldcrc);
  737.         }
  738.         else
  739.             sendline(checksum);
  740.         flushmo();
  741.  
  742.         if (Optiong) {
  743.             firstsec = FALSE; return OK;
  744.         }
  745.         firstch = readline(Rxtimeout);
  746. gotnak:
  747.         switch (firstch) {
  748.         case CAN:
  749.             if(Lastrx == CAN) {
  750. cancan:
  751.                 zperr("Cancelled");  return ERROR;
  752.             }
  753.             break;
  754.         case TIMEOUT:
  755.             zperr("Timeout on sector ACK"); continue;
  756.         case WANTCRC:
  757.             if (firstsec)
  758.                 Crcflg = TRUE;
  759.         case NAK:
  760.             zperr("NAK on sector"); continue;
  761.         case ACK: 
  762.             firstsec=FALSE;
  763.             Totsecs += (cseclen>>7);
  764.             return OK;
  765.         case ERROR:
  766.             zperr("Got burst for sector ACK"); break;
  767.         default:
  768.             zperr("Got %02x for sector ACK", firstch); break;
  769.         }
  770.         for (;;) {
  771.             Lastrx = firstch;
  772.             if ((firstch = readline(Rxtimeout)) == TIMEOUT)
  773.                 break;
  774.             if (firstch == NAK || firstch == WANTCRC)
  775.                 goto gotnak;
  776.             if (firstch == CAN && Lastrx == CAN)
  777.                 goto cancan;
  778.         }
  779.     }
  780.     zperr("Retry Count Exceeded");
  781.     return ERROR;
  782. }
  783.  
  784. /* fill buf with count chars padding with ^Z for CPM */
  785. filbuf(buf, count)
  786. register char *buf;
  787. {
  788.     register m;
  789.  
  790.     m = read(fileno(in), buf, count);
  791.     if (m <= 0)
  792.         return 0;
  793.     while (m < count)
  794.         buf[m++] = 032;
  795.     return count;
  796. }
  797.  
  798. /* Fill buffer with blklen chars */
  799. zfilbuf()
  800. {
  801.     int n;
  802.  
  803. #ifdef TXBSIZE
  804.     vfile("zfilbuf: bytcnt =%lu vpos=%lu blklen=%d", bytcnt, vpos, blklen);
  805.     /* We assume request is within buffer, or just beyond */
  806.     txbuf = Txb + (bytcnt & TXBMASK);
  807.     if (vpos <= bytcnt) {
  808.         n = fread(txbuf, 1, blklen, in);
  809.  
  810.         vpos += n;
  811.         if (n < blklen)
  812.             Eofseen = 1;
  813.         vfile("zfilbuf: n=%d vpos=%lu Eofseen=%d", n, vpos, Eofseen);
  814.         return n;
  815.     }
  816.     if (vpos >= (bytcnt+blklen))
  817.         return blklen;
  818.     /* May be a short block if crash recovery etc. */
  819.     Eofseen = BEofseen;
  820.     return (vpos - bytcnt);
  821. #else
  822.     n = fread(txbuf, 1, blklen, in);
  823.     if (n < blklen) {
  824.         Eofseen = 1;
  825.         vfile("zfilbuf: n=%d vpos=%lu Eofseen=%d", n, vpos, Eofseen);
  826.     }
  827.     return n;
  828. #endif
  829. }
  830.  
  831. #ifdef TXBSIZE
  832. /* Replacement for brain damaged fseek function.  Returns 0==success */
  833. fooseek(fptr, pos, whence)
  834. FILE *fptr;
  835. long pos;
  836. {
  837.     long m, n;
  838.  
  839.     vfile("fooseek: pos =%lu vpos=%lu Canseek=%d", pos, vpos, Canseek);
  840.     /* Seek offset < current buffer */
  841.     if (pos < (vpos -TXBSIZE +1024)) {
  842.         BEofseen = 0;
  843.         if (Canseek > 0) {
  844.             vpos = pos & ~TXBMASK;
  845.             if (vpos >= pos)
  846.                 vpos -= TXBSIZE;
  847.             if (fseek(fptr, vpos, 0))
  848.                 return 1;
  849.         }
  850.         else if (Canseek == 0) {
  851.             if (fseek(fptr, vpos = 0L, 0))
  852.                 return 1;
  853.         } else
  854.             return 1;
  855.         while (vpos < pos) {
  856.             n = fread(Txb, 1, TXBSIZE, fptr);
  857.             vpos += n;
  858.             vfile("n=%d vpos=%ld", n, vpos);
  859.             if (n < TXBSIZE) {
  860.                 BEofseen = 1;
  861.                 break;
  862.             }
  863.         }
  864.         vfile("vpos=%ld", vpos);
  865.         return 0;
  866.     }
  867.     /* Seek offset > current buffer (Crash Recovery, etc.) */
  868.     if (pos > vpos) {
  869.         if (Canseek)
  870.             if (fseek(fptr, vpos = (pos & ~TXBMASK), 0))
  871.                 return 1;
  872.         while (vpos <= pos) {
  873.             txbuf = Txb + (vpos & TXBMASK);
  874.             m = TXBSIZE - (vpos & TXBMASK);
  875.             vfile("m=%ld vpos=%ld", m,vpos);
  876.                 n = fread(txbuf, 1, m, fptr);
  877.             vfile("n=%ld vpos=%ld", n,vpos);
  878.             vpos += n;
  879.             vfile("bo=%d m=%ld vpos=%ld", txbuf-Txb,m,vpos);
  880.             if (n < m) {
  881.                 BEofseen = 1;
  882.                 break;
  883.             }
  884.         }
  885.         return 0;
  886.     }
  887.     /* Seek offset is within current buffer */
  888.     vfile("within buffer: vpos=%ld", vpos);
  889.     return 0;
  890. }
  891. #define fseek fooseek
  892. #endif
  893.  
  894.  
  895. /*
  896.  * substr(string, token) searches for token in string s
  897.  * returns pointer to token within string if found, NULL otherwise
  898.  */
  899. char *
  900. substr(s, t)
  901. register char *s,*t;
  902. {
  903.     register char *ss,*tt;
  904.     /* search for first char of token */
  905.     for (ss=s; *s; s++)
  906.         if (*s == *t)
  907.             /* compare token with substring */
  908.             for (ss=s,tt=t; ;) {
  909.                 if (*tt == 0)
  910.                     return s;
  911.                 if (*ss++ != *tt++)
  912.                     break;
  913.             }
  914.     return NULL;
  915. }
  916.  
  917. char *usinfo[] = {
  918.     "Send Files and Commands with ZMODEM/YMODEM/XMODEM Protocol\n",
  919.     "Usage:    sz [-+abcdefgklLnNuvwyYZ] [-] file ...",
  920.     "\t    zcommand [-egv] COMMAND",
  921.     "\t    zcommandi [-egv] COMMAND",
  922.     "\t    sb [-adfkuv] [-] file ...",
  923.     "\t    sx [-akuv] [-] file",
  924.     ""
  925. };
  926.  
  927. usage()
  928. {
  929.     char **pp;
  930.  
  931.     fprintf(stderr, "\n%s %s for %s by Chuck Forsberg, Omen Technology INC\n",
  932.      Progname, VERSION, OS);
  933.     fprintf(stderr, "\t\t\042The High Reliability Software\042\n");
  934.     for (pp=usinfo; **pp; ++pp)
  935.         fprintf(stderr, "%s\n", *pp);
  936.     fprintf(stderr,"\nCopyright 1994 Omen Technology INC All Rights Reserved\n");
  937.     fprintf(stderr,
  938.      "See sz.doc for option descriptions and licensing information.\n\n");
  939.     fprintf(stderr,
  940.     "This program is intended to interface with terminal programs,\nnot to act as one.\n\n");
  941.     exit(3);
  942. }
  943.  
  944. /*
  945.  * Get the receiver's init parameters
  946.  */
  947. getzrxinit()
  948. {
  949.     register n;
  950.     struct stat f;
  951.  
  952.     for (n=10; --n>=0; ) {
  953.         
  954.         switch (zgethdr(Rxhdr)) {
  955.         case ZCHALLENGE:    /* Echo receiver's challenge numbr */
  956.             stohdr(Rxpos);
  957.             zshhdr(4, ZACK, Txhdr);
  958.             continue;
  959.         case ZCOMMAND:        /* They didn't see out ZRQINIT */
  960.             stohdr(0L);
  961.             zshhdr(4, ZRQINIT, Txhdr);
  962.             continue;
  963.         case ZRINIT:
  964.             Rxflags = 0377 & Rxhdr[ZF0];
  965.             Usevhdrs = Rxhdr[ZF1] & CANVHDR;
  966.             Txfcs32 = (Wantfcs32 && (Rxflags & CANFC32));
  967.             Zctlesc |= Rxflags & TESCCTL;
  968.             Rxbuflen = (0377 & Rxhdr[ZP0])+((0377 & Rxhdr[ZP1])<<8);
  969.             if ( !(Rxflags & CANFDX))
  970.                 Txwindow = 0;
  971.             vfile("Rxbuflen=%d Tframlen=%ld", Rxbuflen, Tframlen);
  972.             signal(SIGINT, SIG_IGN);
  973. #ifdef MODE2OK
  974.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  975. #endif
  976.  
  977. #ifndef READCHECK
  978. #ifndef USG
  979.             /* Use 1024 byte frames if no sample/interrupt */
  980.             if (Rxbuflen < 32 || Rxbuflen > 1024) {
  981.                 Rxbuflen = 1024;
  982.                 vfile("Rxbuflen=%d", Rxbuflen);
  983.             }
  984. #endif
  985. #endif
  986.  
  987.             /* Override to force shorter frame length */
  988.             if (Rxbuflen && (Rxbuflen>Tframlen) && (Tframlen>=32))
  989.                 Rxbuflen = Tframlen;
  990.             if ( !Rxbuflen && (Tframlen>=32) && (Tframlen<=1024))
  991.                 Rxbuflen = Tframlen;
  992.             vfile("Rxbuflen=%d", Rxbuflen);
  993.  
  994.  
  995.             /*
  996.              * If input is not a regular file, force ACK's to
  997.              *  prevent running beyond the buffer limits
  998.              */
  999.             if ( !Command) {
  1000.                 fstat(fileno(in), &f);
  1001.                 if (
  1002. #ifdef POSIX
  1003.                     !S_ISREG(f.st_mode)
  1004. #else
  1005.                     (f.st_mode & S_IFMT) != S_IFREG
  1006. #endif
  1007.                     ) {
  1008.                     Canseek = -1;
  1009. #ifdef TXBSIZE
  1010.                     Txwindow = TXBSIZE - 1024;
  1011.                     Txwspac = TXBSIZE/4;
  1012. #else
  1013.                     return ERROR;
  1014. #endif
  1015.                 }
  1016.             }
  1017.  
  1018.             /* Set initial subpacket length */
  1019.             if (blklen < 1024) {    /* Command line override? */
  1020.                 if (Effbaud > 300)
  1021.                     blklen = 256;
  1022.                 if (Effbaud > 1200)
  1023.                     blklen = 512;
  1024.                 if (Effbaud > 2400)
  1025.                     blklen = 1024;
  1026.             }
  1027.             if (Rxbuflen && blklen>Rxbuflen)
  1028.                 blklen = Rxbuflen;
  1029.             if (blkopt && blklen > blkopt)
  1030.                 blklen = blkopt;
  1031.             vfile("Rxbuflen=%d blklen=%d", Rxbuflen, blklen);
  1032.             vfile("Txwindow = %u Txwspac = %d", Txwindow, Txwspac);
  1033.  
  1034.  
  1035.             if (Lztrans == ZTRLE && (Rxflags & CANRLE))
  1036.                 Txfcs32 = 2;
  1037.             else
  1038.                 Lztrans = 0;
  1039.  
  1040.             return (sendzsinit());
  1041.         case ZCAN:
  1042.         case TIMEOUT:
  1043.             return ERROR;
  1044.         case ZRQINIT:
  1045.             if (Rxhdr[ZF0] == ZCOMMAND)
  1046.                 continue;
  1047.         default:
  1048.             zshhdr(4, ZNAK, Txhdr);
  1049.             continue;
  1050.         }
  1051.     }
  1052.     return ERROR;
  1053. }
  1054.  
  1055. /* Send send-init information */
  1056. sendzsinit()
  1057. {
  1058.     register c;
  1059.  
  1060.     if (Myattn[0] == '\0' && (!Zctlesc || (Rxflags & TESCCTL)))
  1061.         return OK;
  1062.     errors = 0;
  1063.     for (;;) {
  1064.         stohdr(0L);
  1065. #ifdef ALTCANOFF
  1066.         Txhdr[ALTCOFF] = ALTCANOFF;
  1067. #endif
  1068.         if (Zctlesc) {
  1069.             Txhdr[ZF0] |= TESCCTL; zshhdr(4, ZSINIT, Txhdr);
  1070.         }
  1071.         else
  1072.             zsbhdr(4, ZSINIT, Txhdr);
  1073.         zsdata(Myattn, ZATTNLEN, ZCRCW);
  1074.         c = zgethdr(Rxhdr);
  1075.         switch (c) {
  1076.         case ZCAN:
  1077.             return ERROR;
  1078.         case ZACK:
  1079.             return OK;
  1080.         default:
  1081.             if (++errors > 19)
  1082.                 return ERROR;
  1083.             continue;
  1084.         }
  1085.     }
  1086. }
  1087.  
  1088. /* Send file name and related info */
  1089. zsendfile(buf, blen)
  1090. char *buf;
  1091. {
  1092.     register c;
  1093.     register unsigned long crc;
  1094.     long lastcrcrq = -1;
  1095.  
  1096.     for (errors=0; ++errors<11;) {
  1097.         Txhdr[ZF0] = Lzconv;    /* file conversion request */
  1098.         Txhdr[ZF1] = Lzmanag;    /* file management request */
  1099.         if (Lskipnocor)
  1100.             Txhdr[ZF1] |= ZMSKNOLOC;
  1101.         Txhdr[ZF2] = Lztrans;    /* file transport request */
  1102.         Txhdr[ZF3] = 0;
  1103.         zsbhdr(4, ZFILE, Txhdr);
  1104.         zsdata(buf, blen, ZCRCW);
  1105. again:
  1106.         c = zgethdr(Rxhdr);
  1107.         switch (c) {
  1108.         case ZRINIT:
  1109.             while ((c = readline(50)) > 0)
  1110.                 if (c == ZPAD) {
  1111.                     goto again;
  1112.                 }
  1113.             continue;
  1114.         case ZCAN:
  1115.         case TIMEOUT:
  1116.         case ZABORT:
  1117.         case ZFIN:
  1118.             sprintf(endmsg, "Got %s on pathname", frametypes[c+FTOFFSET]);
  1119.             return ERROR;
  1120.         default:
  1121.             sprintf(endmsg, "Got %d frame type on pathname", c);
  1122.             continue;
  1123.         case ERROR:
  1124.         case ZNAK:
  1125.             continue;
  1126.         case ZCRC:
  1127.             if (Rxpos != lastcrcrq) {
  1128.                 lastcrcrq = Rxpos;
  1129.                 crc = 0xFFFFFFFFL;
  1130.                 if (Canseek >= 0) {
  1131.                     fseek(in, 0L, 0);
  1132.                     while (((c = getc(in)) != EOF) && --lastcrcrq)
  1133.                         crc = UPDC32(c, crc);
  1134.                     crc = ~crc;
  1135.                     clearerr(in);    /* Clear possible EOF */
  1136.                     lastcrcrq = Rxpos;
  1137.                 }
  1138.             }
  1139.             stohdr(crc);
  1140.             zsbhdr(4, ZCRC, Txhdr);
  1141.             goto again;
  1142.         case ZFERR:
  1143.         case ZSKIP:
  1144.             sprintf(endmsg, "File skipped by receiver request");
  1145.             fclose(in); return c;
  1146.         case ZRPOS:
  1147.             /*
  1148.              * Suppress zcrcw request otherwise triggered by
  1149.              * lastyunc==bytcnt
  1150.              */
  1151.             if (fseek(in, Rxpos, 0))
  1152.                 return ERROR;
  1153.             Lastsync = (bytcnt = Txpos = Lrxpos = Rxpos) -1;
  1154.             return zsendfdata();
  1155.         }
  1156.     }
  1157.     fclose(in); return ERROR;
  1158. }
  1159.  
  1160. /* Send the data in the file */
  1161. zsendfdata()
  1162. {
  1163.     register c, e, n;
  1164.     register newcnt;
  1165.     register long tcount = 0;
  1166.     int junkcount;        /* Counts garbage chars received by TX */
  1167.     static int tleft = 6;    /* Counter for test mode */
  1168.  
  1169.     junkcount = 0;
  1170.     Beenhereb4 = FALSE;
  1171. somemore:
  1172.     if (setjmp(intrjmp)) {
  1173. waitack:
  1174.         junkcount = 0;
  1175.         c = getinsync(0);
  1176. gotack:
  1177.         switch (c) {
  1178.         default:
  1179.         case ZCAN:
  1180.             fclose(in);
  1181.             return ERROR;
  1182.         case ZRINIT:
  1183.             fclose(in);
  1184.             return ZSKIP;
  1185.         case ZSKIP:
  1186.             fclose(in);
  1187.             return c;
  1188.         case ZACK:
  1189.         case ZRPOS:
  1190.             break;
  1191.         }
  1192. #ifdef READCHECK
  1193.         /*
  1194.          * If the reverse channel can be tested for data,
  1195.          *  this logic may be used to detect error packets
  1196.          *  sent by the receiver, in place of setjmp/longjmp
  1197.          *  rdchk(Tty) returns non 0 if a character is available
  1198.          */
  1199.         while (rdchk(Tty)) {
  1200. #ifdef EATSIT
  1201.             switch (checked)
  1202. #else
  1203.             switch (readline(1))
  1204. #endif
  1205.             {
  1206.             case CAN:
  1207.             case ZPAD:
  1208.                 c = getinsync(1);
  1209.                 goto gotack;
  1210.             case XOFF:        /* Wait a while for an XON */
  1211.                 readline(100);
  1212.             }
  1213.         }
  1214. #endif
  1215.     }
  1216.  
  1217.     signal(SIGINT, onintr);
  1218.     newcnt = Rxbuflen;
  1219.     Txwcnt = 0;
  1220.     stohdr(Txpos);
  1221.     zsbhdr(4, ZDATA, Txhdr);
  1222.  
  1223.     /*
  1224.      * Special testing mode.  This should force receiver to Attn,ZRPOS
  1225.      *  many times.  Each time the signal should be caught, causing the
  1226.      *  file to be started over from the beginning.
  1227.      */
  1228.     if (Test) {
  1229.         if ( --tleft)
  1230.             while (tcount < 20000) {
  1231.                 printf(qbf); fflush(stdout);
  1232.                 tcount += strlen(qbf);
  1233. #ifdef READCHECK
  1234.                 while (rdchk(Tty)) {
  1235. #ifdef EATSIT
  1236.                     switch (checked)
  1237. #else
  1238.                     switch (readline(1))
  1239. #endif
  1240.                     {
  1241.                     case CAN:
  1242.                     case ZPAD:
  1243.                         goto waitack;
  1244.                     case XOFF:    /* Wait for XON */
  1245.                         readline(100);
  1246.                     }
  1247.                 }
  1248. #endif
  1249.             }
  1250.         signal(SIGINT, SIG_IGN); canit();
  1251.         sleep(3); purgeline(); mode(0);
  1252.         printf("\nsz: Tcount = %ld\n", tcount);
  1253.         if (tleft) {
  1254.             printf("ERROR: Interrupts Not Caught\n");
  1255.             exit(1);
  1256.         }
  1257.         exit(0);
  1258.     }
  1259.  
  1260.     do {
  1261.         n = zfilbuf();
  1262.         if (Eofseen)
  1263.             e = ZCRCE;
  1264.         else if (junkcount > 3)
  1265.             e = ZCRCW;
  1266.         else if (bytcnt == Lastsync)
  1267.             e = ZCRCW;
  1268.         else if (Rxbuflen && (newcnt -= n) <= 0)
  1269.             e = ZCRCW;
  1270.         else if (Txwindow && (Txwcnt += n) >= Txwspac) {
  1271.             Txwcnt = 0;  e = ZCRCQ;
  1272.         } else
  1273.             e = ZCRCG;
  1274.         if (Verbose>1)
  1275.             fprintf(stderr, "\r%7ld ZMODEM%s    ",
  1276.               Txpos, Crc32t?" CRC-32":"");
  1277.         zsdata(txbuf, n, e);
  1278.         bytcnt = Txpos += n;
  1279.         if (e == ZCRCW)
  1280.             goto waitack;
  1281. #ifdef READCHECK
  1282.         /*
  1283.          * If the reverse channel can be tested for data,
  1284.          *  this logic may be used to detect error packets
  1285.          *  sent by the receiver, in place of setjmp/longjmp
  1286.          *  rdchk(Tty) returns non 0 if a character is available
  1287.          */
  1288.         fflush(stdout);
  1289.         while (rdchk(Tty)) {
  1290. #ifdef EATSIT
  1291.             switch (checked)
  1292. #else
  1293.             switch (readline(1))
  1294. #endif
  1295.             {
  1296.             case CAN:
  1297.             case ZPAD:
  1298.                 c = getinsync(1);
  1299.                 if (c == ZACK)
  1300.                     break;
  1301.                 /* zcrce - dinna wanna starta ping-pong game */
  1302.                 zsdata(txbuf, 0, ZCRCE);
  1303.                 goto gotack;
  1304.             case XOFF:        /* Wait a while for an XON */
  1305.                 readline(100);
  1306.             default:
  1307.                 ++junkcount;
  1308.             }
  1309.         }
  1310. #endif    /* READCHECK */
  1311.         if (Txwindow) {
  1312.             while ((tcount = (Txpos - Lrxpos)) >= Txwindow) {
  1313.                 vfile("%ld window >= %u", tcount, Txwindow);
  1314.                 if (e != ZCRCQ)
  1315.                     zsdata(txbuf, 0, e = ZCRCQ);
  1316.                 c = getinsync(1);
  1317.                 if (c != ZACK) {
  1318.                     zsdata(txbuf, 0, ZCRCE);
  1319.                     goto gotack;
  1320.                 }
  1321.             }
  1322.             vfile("window = %ld", tcount);
  1323.         }
  1324.     } while (!Eofseen);
  1325.     signal(SIGINT, SIG_IGN);
  1326.  
  1327.     for (;;) {
  1328.         stohdr(Txpos);
  1329.         zsbhdr(4, ZEOF, Txhdr);
  1330. egotack:
  1331.         switch (getinsync(0)) {
  1332.         case ZACK:
  1333.             goto egotack;
  1334.         case ZNAK:
  1335.             continue;
  1336.         case ZRPOS:
  1337.             goto somemore;
  1338.         case ZRINIT:
  1339.             fclose(in);
  1340.             return OK;
  1341.         case ZSKIP:
  1342.             fclose(in);
  1343.             sprintf(endmsg, "File skipped by receiver request");
  1344.             return c;
  1345.         default:
  1346.             sprintf(endmsg, "Got %d trying to send end of file", c);
  1347.         case ERROR:
  1348.             fclose(in);
  1349.             return ERROR;
  1350.         }
  1351.     }
  1352. }
  1353.  
  1354. /*
  1355.  * Respond to receiver's complaint, get back in sync with receiver
  1356.  */
  1357. getinsync(flag)
  1358. {
  1359.     register c;
  1360.  
  1361.     for (;;) {
  1362.         if (Test) {
  1363.             printf("\r\n\n\n***** Signal Caught *****\r\n");
  1364.             Rxpos = 0; c = ZRPOS;
  1365.         } else
  1366.             c = zgethdr(Rxhdr);
  1367.         switch (c) {
  1368.         case ZCAN:
  1369.         case ZABORT:
  1370.         case ZFIN:
  1371.         case TIMEOUT:
  1372.             sprintf(endmsg, "Got %s sending data", frametypes[c+FTOFFSET]);
  1373.             return ERROR;
  1374.         case ZRPOS:
  1375.             if (Rxpos > bytcnt) {
  1376.                 sprintf(endmsg, "Nonstandard Protocol");
  1377.                 return ZRPOS;
  1378.             }
  1379.             /* ************************************* */
  1380.             /*  If sending to a buffered modem, you  */
  1381.             /*   might send a break at this point to */
  1382.             /*   dump the modem's buffer.         */
  1383.             clearerr(in);    /* In case file EOF seen */
  1384.             if (fseek(in, Rxpos, 0)) {
  1385.                 sprintf(endmsg, "Bad Seek");
  1386.                 return ERROR;
  1387.             }
  1388.             Eofseen = 0;
  1389.             bytcnt = Lrxpos = Txpos = Rxpos;
  1390.             if (Lastsync == Rxpos) {
  1391.                 if (++Beenhereb4 > 12) {
  1392.                     sprintf(endmsg, "Can't send block");
  1393.                     return ERROR;
  1394.                 }
  1395.                 if (Beenhereb4 > 4)
  1396.                     if (blklen > 32)
  1397.                         blklen /= 2;
  1398.             }
  1399.             Lastsync = Rxpos;
  1400.             return c;
  1401.         case ZACK:
  1402.             Lrxpos = Rxpos;
  1403.             if (flag || Txpos == Rxpos)
  1404.                 return ZACK;
  1405.             continue;
  1406.         case ZRINIT:
  1407.             return c;
  1408.         case ZSKIP:
  1409.             sprintf(endmsg, "File skipped by receiver request");
  1410.             return c;
  1411.         case ERROR:
  1412.         default:
  1413.             zsbhdr(4, ZNAK, Txhdr);
  1414.             continue;
  1415.         }
  1416.     }
  1417. }
  1418.  
  1419.  
  1420. /* Send command and related info */
  1421. zsendcmd(buf, blen)
  1422. char *buf;
  1423. {
  1424.     register c;
  1425.     long cmdnum;
  1426.  
  1427.     cmdnum = getpid();
  1428.     errors = 0;
  1429.     for (;;) {
  1430.         stohdr(cmdnum);
  1431.         Txhdr[ZF0] = Cmdack1;
  1432.         zsbhdr(4, ZCOMMAND, Txhdr);
  1433.         zsdata(buf, blen, ZCRCW);
  1434. listen:
  1435.         Rxtimeout = 100;        /* Ten second wait for resp. */
  1436.         Usevhdrs = 0;        /* Allow rx to send fixed len headers */
  1437.         c = zgethdr(Rxhdr);
  1438.  
  1439.         switch (c) {
  1440.         case ZRINIT:
  1441.             goto listen;    /* CAF 8-21-87 */
  1442.         case ERROR:
  1443.         case GCOUNT:
  1444.         case TIMEOUT:
  1445.             if (++errors > 11)
  1446.                 return ERROR;
  1447.             continue;
  1448.         case ZCAN:
  1449.         case ZABORT:
  1450.         case ZFIN:
  1451.         case ZSKIP:
  1452.         case ZRPOS:
  1453.             return ERROR;
  1454.         default:
  1455.             if (++errors > 20)
  1456.                 return ERROR;
  1457.             continue;
  1458.         case ZCOMPL:
  1459.             Exitcode = Rxpos;
  1460.             saybibi();
  1461.             return OK;
  1462.         case ZRQINIT:
  1463.             vfile("******** RZ *******");
  1464.             system("rz");
  1465.             vfile("******** SZ *******");
  1466.             goto listen;
  1467.         }
  1468.     }
  1469. }
  1470.  
  1471. /*
  1472.  * If called as sb use YMODEM protocol
  1473.  */
  1474. chkinvok(s)
  1475. char *s;
  1476. {
  1477.     register char *p;
  1478.  
  1479.     p = s;
  1480.     while (*p == '-')
  1481.         s = ++p;
  1482.     while (*p)
  1483.         if (*p++ == '/')
  1484.             s = p;
  1485.     if (*s == 'v') {
  1486.         Verbose=1; ++s;
  1487.     }
  1488.     Progname = s;
  1489.     if (s[0]=='z' && s[1] == 'c') {
  1490.         Command = TRUE;
  1491.         if (s[8] == 'i')
  1492.             Cmdack1 = ZCACK1;
  1493.     }
  1494.     if (s[0]=='s' && s[1]=='b') {
  1495.         Nozmodem = TRUE; blklen=1024;
  1496.     }
  1497.     if (s[0]=='s' && s[1]=='x') {
  1498.         Modem2 = TRUE;
  1499.     }
  1500. }
  1501.  
  1502. countem(argc, argv)
  1503. register char **argv;
  1504. {
  1505.     struct stat f;
  1506.  
  1507.     for (Totalleft = 0, Filesleft = 0; --argc >=0; ++argv) {
  1508.         f.st_size = -1;
  1509.         if (Verbose>2) {
  1510.             fprintf(stderr, "\nCountem: %03d %s ", argc, *argv);
  1511.             fflush(stderr);
  1512.         }
  1513.         if (access(*argv, 04) >= 0 && stat(*argv, &f) >= 0) {
  1514.             ++Filesleft;  Totalleft += f.st_size;
  1515.         }
  1516.         if (Verbose>2)
  1517.             fprintf(stderr, " %ld", f.st_size);
  1518.     }
  1519.     if (Verbose>2)
  1520.         fprintf(stderr, "\ncountem: Total %d %ld\n",
  1521.           Filesleft, Totalleft);
  1522. }
  1523.  
  1524. chartest(m)
  1525. {
  1526.     register n;
  1527.  
  1528.     mode(m);
  1529.     printf("\r\n\nCharacter Transparency Test Mode %d\r\n", m);
  1530.     printf("If Pro-YAM/ZCOMM is not displaying ^M hit ALT-V NOW.\r\n");
  1531.     printf("Hit Enter.\021");  fflush(stdout);
  1532.     readline(500);
  1533.  
  1534.     for (n = 0; n < 256; ++n) {
  1535.         if (!(n%8))
  1536.             printf("\r\n");
  1537.         printf("%02x ", n);  fflush(stdout);
  1538.         sendline(n);    flushmo();
  1539.         printf("  ");  fflush(stdout);
  1540.         if (n == 127) {
  1541.             printf("Hit Enter.\021");  fflush(stdout);
  1542.             readline(500);
  1543.             printf("\r\n");  fflush(stdout);
  1544.         }
  1545.     }
  1546.     printf("\021\r\nEnter Characters, echo is in hex.\r\n");
  1547.     printf("Hit SPACE or pause 40 seconds for exit.\r\n");
  1548.  
  1549.     while (n != TIMEOUT && n != ' ') {
  1550.         n = readline(400);
  1551.         printf("%02x\r\n", n);
  1552.         fflush(stdout);
  1553.     }
  1554.     printf("\r\nMode %d character transparency test ends.\r\n", m);
  1555.     fflush(stdout);
  1556. }
  1557.  
  1558. /* End of sz.c */
  1559.